package com.zhizuobiao.coura.stockhelper;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.zhizuobiao.coura.application.MyApplication;
import com.zhizuobiao.coura.stock.HAStockService;
import com.zhizuobiao.coura.stock.HHStockService;
import com.zhizuobiao.coura.stock.HKStockService;
import com.zhizuobiao.coura.stock.HkStock;
import com.zhizuobiao.coura.stock.HsStock;
import com.zhizuobiao.coura.stock.SHStockService;
import com.zhizuobiao.coura.stock.UsaStock;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by coura on 2016/12/20.
 */

public class StockActivity extends AppCompatActivity {

    @BindView(R.id.tv_stockName_detail)
    TextView tvStockNameDetail;
    @BindView(R.id.tv_stockSymbol)
    TextView tvStockSymbol;
    @BindView(R.id.tv_nowPrice)
    TextView tvNowPrice;
    @BindView(R.id.tv_increasePer)
    TextView tvIncreasePer;
    @BindView(R.id.tv_increase)
    TextView tvIncrease;
    @BindView(R.id.tv_todayStartPri)
    TextView tvTodayStartPri;
    @BindView(R.id.tv_minUrl)
    TextView tvMinUrl;
    @BindView(R.id.tv_dayUrl)
    TextView tvDayUrl;
    @BindView(R.id.tv_weekUrl)
    TextView tvWeekUrl;
    @BindView(R.id.tv_minthUrl)
    TextView tvMinthUrl;
    @BindView(R.id.K_line1)
    View KLine1;
    @BindView(R.id.K_line2)
    View KLine2;
    @BindView(R.id.K_line3)
    View KLine3;
    @BindView(R.id.K_line4)
    View KLine4;
    @BindView(R.id.iv_stockTable)
    ImageView ivStockTable;
    @BindView(R.id.tv_traAmount)
    TextView tvTraAmount;
    @BindView(R.id.tv_traNumber)
    TextView tvTraNumber;
    @BindView(R.id.tv_todayMax)
    TextView tvTodayMax;
    @BindView(R.id.tv_todayMin)
    TextView tvTodayMin;
    @BindView(R.id.tv_yestodEndPri)
    TextView tvYestodEndPri;
    private MyApplication myApplication;
    private HKStockService hkStockService;
    private HAStockService haStockService;
    private SHStockService shStockService;
    private HHStockService hhStockService;
    private UsaStock usaStock;
    private HkStock hkStock;
    private HsStock hsStock;
    private int num;
    private static final String APPKEY = "81b0a782ae1a008d322f47bbb5907458";      //聚合数据Key

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stock_detail);
        ButterKnife.bind(this);
        init();
    }

    /**
     * 初始化
     */
    private void init() {
        myApplication = (MyApplication) getApplication();
        hkStockService = myApplication.getHkStockService();
        haStockService = myApplication.getHaStockService();
        shStockService = myApplication.getShStockService();
        hhStockService = myApplication.getHhStockService();
        Intent intent = getIntent();
        num = intent.getIntExtra("num", -1);
        String gid = intent.getStringExtra("gid");
        loadStock(num,gid);
    }

    /**
     * 加载具体股票信息
     * @param num
     * @param gid
     */
    private void loadStock(int num, String gid) {
        switch (num) {
            case 0:
                loadHkStock(gid);
                break;
            case 1:
                loadUsaStock(gid);
                break;
            case 2:
                loadHsStock(gid);
                break;
        }
    }

    /**
     * 加载指定港股的具体信息
     * @param gid
     */
    private void loadHkStock(String gid){
        Call<HkStock> hkStockCall = hkStockService.getStock(gid,APPKEY);
        hkStockCall.enqueue(new Callback<HkStock>() {
            @Override
            public void onResponse(Call<HkStock> call, Response<HkStock> response) {
                hkStock = response.body();
                if(hkStock.getError_code() == 0){
                    HkStock.ResultBean.DataBean dateBean = hkStock.getResult().get(0).getData();
                    tvStockNameDetail.setText(dateBean.getName());
                    tvStockSymbol.setText(dateBean.getGid());
                    tvNowPrice.setText(dateBean.getLastestpri());
                    tvIncreasePer.setText(dateBean.getLimit());
                    tvIncrease.setText(dateBean.getUppic());
                    tvTodayStartPri.setText("今开："+dateBean.getOpenpri());
                    tvYestodEndPri.setText("昨收："+dateBean.getFormpri());
                    tvTraAmount.setText("成交额："+dateBean.getTraAmount());
                    tvTraNumber.setText("成交量："+dateBean.getTraNumber());
                    tvTodayMax.setText("最高："+dateBean.getMaxpri());
                    tvTodayMin.setText("最低："+dateBean.getMinpri());
                    Glide.with(StockActivity.this).load(hkStock.getResult().get(0).getGopicture().getMinurl()).into(ivStockTable);
                }
            }
            @Override
            public void onFailure(Call<HkStock> call, Throwable t) {

            }
        });
    }

    /**
     * 加载指定美股的详细信息
     * @param gid
     */
    private void loadUsaStock(String gid) {
        Call<UsaStock> usaStockCall = haStockService.getStock(gid, APPKEY);
        usaStockCall.enqueue(new Callback<UsaStock>() {
            @Override
            public void onResponse(Call<UsaStock> call, Response<UsaStock> response) {
                usaStock = response.body();
                if (usaStock.getError_code() == 0 ) {
                    UsaStock.ResultBean.DataBean dateBean = usaStock.getResult().get(0).getData();
                    tvStockNameDetail.setText(dateBean.getName());
                    tvStockSymbol.setText(dateBean.getGid());
                    tvNowPrice.setText(dateBean.getLastestpri());
                    tvIncreasePer.setText(dateBean.getLimit());
                    tvIncrease.setText(dateBean.getUppic());
                    tvTodayStartPri.setText("今开："+dateBean.getOpenpri());
                    tvYestodEndPri.setText("昨收："+dateBean.getFormpri());
                    tvTraAmount.setText("成交额："+dateBean.getTraAmount());
                    tvTraNumber.setText("成交量："+dateBean.getAvgTraNumber());
                    tvTodayMax.setText("最高："+dateBean.getMaxpri());
                    tvTodayMin.setText("最低："+dateBean.getMinpri());
                    Glide.with(StockActivity.this).load(usaStock.getResult().get(0).getGopicture().getMinurl()).into(ivStockTable);
                }else{
                    Toast.makeText(StockActivity.this,"数据加载出错",Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(StockActivity.this,MainActivity.class);
                    startActivity(intent);
                    StockActivity.this.finish();
                }
            }
            @Override
            public void onFailure(Call<UsaStock> call, Throwable t) {
                Toast.makeText(StockActivity.this,"数据加载出错，请查看网络是否连接",Toast.LENGTH_LONG).show();
            }
        });
    }

    /**
     * 加载指定沪深股票信息
     * @param gid
     */
    private void loadHsStock(String gid){
        Call<HsStock> hsStockCall = shStockService.getStock(gid,APPKEY);
        hsStockCall.enqueue(new Callback<HsStock>() {
            @Override
            public void onResponse(Call<HsStock> call, Response<HsStock> response) {
                hsStock = response.body();
                if(hsStock.getError_code() == 0){
                    HsStock.ResultBean.DataBean dateBean = hsStock.getResult().get(0).getData();
                    tvStockNameDetail.setText(dateBean.getName());
                    tvStockSymbol.setText(dateBean.getGid());
                    tvNowPrice.setText(dateBean.getNowPri());
                    tvIncreasePer.setText(dateBean.getIncrePer());
                    tvIncrease.setText(dateBean.getIncrease());
                    tvTodayStartPri.setText("今开："+dateBean.getTodayStartPri());
                    tvYestodEndPri.setText("昨收："+dateBean.getYestodEndPri());
                    tvTraAmount.setText("成交额："+dateBean.getTraAmount());
                    tvTraNumber.setText("成交量："+dateBean.getTraNumber());
                    tvTodayMax.setText("最高："+dateBean.getTodayMax());
                    tvTodayMin.setText("最低："+dateBean.getTodayMin());
                    Glide.with(StockActivity.this).load(hsStock.getResult().get(0).getGopicture().getMinurl()).into(ivStockTable);
                }
            }
            @Override
            public void onFailure(Call<HsStock> call, Throwable t) {
                Toast.makeText(StockActivity.this,"数据加载出错，请查看网络是否连接",Toast.LENGTH_LONG).show();
            }
        });
    }

    /**
     * K曲线点击事件
     * @param view
     */
    @OnClick({R.id.tv_minUrl, R.id.tv_dayUrl, R.id.tv_weekUrl, R.id.tv_minthUrl, R.id.iv_back})
    public void onClick(View view) {
        claerState();
        switch (view.getId()) {
            case R.id.tv_minUrl:
                tvMinUrl.setTextColor(getResources().getColor(R.color.colorBlue));
                KLine1.setBackgroundColor(getResources().getColor(R.color.colorBlue));
                switch (num){
                    case 0:
                        Glide.with(StockActivity.this).load(hkStock.getResult().get(0).getGopicture().getMinurl()).into(ivStockTable);
                        break;
                    case 1:
                        Glide.with(StockActivity.this).load(usaStock.getResult().get(0).getGopicture().getMinurl()).into(ivStockTable);
                        break;
                    case 2:
                        Glide.with(StockActivity.this).load(hsStock.getResult().get(0).getGopicture().getMinurl()).into(ivStockTable);
                        break;
                }
                break;
            case R.id.tv_dayUrl:
                tvDayUrl.setTextColor(getResources().getColor(R.color.colorBlue));
                KLine2.setBackgroundColor(getResources().getColor(R.color.colorBlue));
                switch (num){
                    case 0:
                        Glide.with(StockActivity.this).load(hkStock.getResult().get(0).getGopicture().getDayurl()).into(ivStockTable);
                        break;
                    case 1:
                        Glide.with(StockActivity.this).load(usaStock.getResult().get(0).getGopicture().getDayurl()).into(ivStockTable);
                        break;
                    case 2:
                        Glide.with(StockActivity.this).load(hsStock.getResult().get(0).getGopicture().getDayurl()).into(ivStockTable);
                        break;
                }
                break;
            case R.id.tv_weekUrl:
                tvWeekUrl.setTextColor(getResources().getColor(R.color.colorBlue));
                KLine3.setBackgroundColor(getResources().getColor(R.color.colorBlue));
                switch (num){
                    case 0:
                        Glide.with(StockActivity.this).load(hkStock.getResult().get(0).getGopicture().getWeekurl()).into(ivStockTable);
                        break;
                    case 1:
                        Glide.with(StockActivity.this).load(usaStock.getResult().get(0).getGopicture().getWeekurl()).into(ivStockTable);
                        break;
                    case 2:
                        Glide.with(StockActivity.this).load(hsStock.getResult().get(0).getGopicture().getWeekurl()).into(ivStockTable);
                        break;
                }
                break;
            case R.id.tv_minthUrl:
                tvMinthUrl.setTextColor(getResources().getColor(R.color.colorBlue));
                KLine4.setBackgroundColor(getResources().getColor(R.color.colorBlue));
                switch (num){
                    case 0:
                        Glide.with(StockActivity.this).load(hkStock.getResult().get(0).getGopicture().getMonthurl()).into(ivStockTable);
                        break;
                    case 1:
                        Glide.with(StockActivity.this).load(usaStock.getResult().get(0).getGopicture().getMonthurl()).into(ivStockTable);
                        break;
                    case 2:
                        Glide.with(StockActivity.this).load(hsStock.getResult().get(0).getGopicture().getMonthurl()).into(ivStockTable);
                        break;
                }
                break;
            case R.id.iv_back:
                Intent intent = new Intent(this,MainActivity.class);
                startActivity(intent);
                this.finish();    //????
                break;
        }
    }

    /**
     * 清除状态
     */
    private void claerState(){
        tvMinUrl.setTextColor(getResources().getColor(R.color.colorGrey));
        tvDayUrl.setTextColor(getResources().getColor(R.color.colorGrey));
        tvWeekUrl.setTextColor(getResources().getColor(R.color.colorGrey));
        tvMinthUrl.setTextColor(getResources().getColor(R.color.colorGrey));
        KLine1.setBackgroundColor(getResources().getColor(R.color.colorWhite));
        KLine2.setBackgroundColor(getResources().getColor(R.color.colorWhite));
        KLine3.setBackgroundColor(getResources().getColor(R.color.colorWhite));
        KLine4.setBackgroundColor(getResources().getColor(R.color.colorWhite));
    }

}
